﻿using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Threading;
using System.Globalization;
using Microsoft.SharePoint.Utilities;

using System.ComponentModel;
using VPLink2013.Framework;
using System;
using System.Text;
using System.IO;

namespace VPLink.WebParts
{
    [ToolboxItemAttribute(false)]
    public class SaisieWebPart : System.Web.UI.WebControls.WebParts.WebPart
    {
        #region Variables
        private string _listName = "";
        private string _siteSharepoint = "";
        private string _urlPageDetail = "";
        private string _modeAffichage = "";

        #endregion

        #region Property Zone

        [Category("Vente privée")]
        [WebPartStorage(Storage.Shared)]
        [WebDisplayName("Site Sharepoint VP")]
        [WebDescription("Site Sharepoint VP")]
        [WebBrowsable(true)]
        [Personalizable(true)]
        public string SiteSharepoint
        {
            get
            {
                return _siteSharepoint;
            }
            set
            {
                _siteSharepoint = SPContext.Current.Site.Url;
            }
        }

        [Category("Vente privée")]
        [WebPartStorage(Storage.Shared)]
        [WebDisplayName("Liste Sharepoint VP")]
        [WebDescription("Liste Sharepoint VP")]
        [WebBrowsable(true)]
        [Personalizable(true)]
        public string ListName
        {
            get
            {
                return _listName;
            }
            set
            {
                _listName = value;
            }
        }

        [Category("Vente privée")]
        [WebPartStorage(Storage.Shared)]
        [WebDisplayName("URL page detail")]
        [WebDescription("URL page detail")]
        [WebBrowsable(true)]
        [Personalizable(true)]
        public string UrlPageDetail
        {
            get
            {
                return _urlPageDetail;
            }
            set
            {
                _urlPageDetail = value;
            }
        }

        [Category("Vente privée")]
        [WebPartStorage(Storage.Shared)]
        [WebDisplayName("Mode affichage")]
        [WebDescription("Mode affichage")]
        [WebBrowsable(true)]
        [Personalizable(true)]
        public string ModeAffichage
        {
            get
            {
                return _modeAffichage;
            }
            set
            {
                _modeAffichage = value;
            }
        }
        #endregion


        #region Render Web part

        protected override void Render(HtmlTextWriter output)
        {
            if (_listName == "")
            {
                output.Write("Il faut configurer le site et la liste SharePoint");
                return;
            }

            //MyTrace.Initialise("wp" + listName + ".xml");
            ContextTrace.PathTemp = @"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\TEMPLATE\LAYOUTS\VPLink2013\WebPartSaisie\Temp";
            ContextTrace.AddTrace("Start");

            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("fr-FR", false);
            string strText = string.Empty;
            try
            {
                HtmlWriter htm = new HtmlWriter(Context, _siteSharepoint, _listName, _urlPageDetail, _modeAffichage);
                htm.SetAction();
                strText = htm.GetHtml();
            }
            catch (System.Exception exc)
            {
                strText = "Erreur : " + exc.Message + "<br>" + exc.StackTrace;
                ContextTrace.AddTrace("Erreur : " + strText);
            }
            ContextTrace.AddTrace("End");
            //ContextTrace.Save(pathTemp, "SaisieNews");

            //strText += System.Security.Principal.WindowsIdentity.GetCurrent().Name;
            output.Write(strText);
        }
        #endregion
    }
}
